load('perhourttweet.mat');
load('DJIAhalfhour.mat');
numDays=100;
numPoints=size(stocksval,2);
Y=[];
numTokens=8;
for ii=1:numDays
    startdate=char(tweetdate(ii,:));
    findindex=0;
    for j=1:length(stockdate)
        if isequal(char(stockdate(j,:)),startdate)
            findindex=j;
        end
    end
    Y=[Y;stocksval(findindex,:)];
end

X=perhourtweets(:,1:numDays*24);
% for i=1:size(X,2)
%     X(:,i)=X(:,i)/sum(X(:,i));
% end

Y_day=sum(Y,2)/numPoints;
W_tweet=24;
W_stock=6;
Theta_stock=zeros(W_stock,1);
Theta_day=0;
Theta_tweet=zeros(W_tweet,numTokens);
Theta=zeros(W_stock+1+numTokens*W_tweet,1);

%LMS
X_LMS=zeros((numDays-1)*(numPoints-W_stock+1)/2,length(Theta));
Y_LMS=zeros(size(X_LMS,1),1);
count=0;
for i=2:numDays
    for t=W_stock+1:2:numPoints
        count=count+1;
        Y_LMS(count)=Y(i,t);
        X_LMS(count,:)=[Y(i,t-1:-1:t-W_stock),Y_day(i-1),reshape(X(1:numTokens,(i-1)*24+(t+1)/2+8:-1:(i-1)*24+(t+1)/2+9-W_tweet)',1,numTokens*W_tweet)]; 
    end
end
Theta=inv(X_LMS'*X_LMS)*X_LMS'*Y_LMS;
Y_predict=X_LMS*Theta;
Y_temp_predict=reshape(Y_predict,(numDays-1),(numPoints-W_stock+1)/2);
Y_temp_predict=[Y(2:numDays,W_stock-1),Y_temp_predict];
stockgrowth_predict=diff(Y_temp_predict,1,2);
stockgrowth_predict=(stockgrowth_predict>0);
Y_temp=Y(2:numDays,W_stock-1:2:numPoints);
stockgrowth=diff(Y_temp,1,2);
stockgrowth=(stockgrowth>0);
growtherror=sum(sum(abs(stockgrowth-stockgrowth_predict)))/(size(stockgrowth,1)*size(stockgrowth,2));

train_error=sum((Y_predict-Y_LMS).^2)/length(Y_LMS);


%Compute Individual Thetas
Theta_stock=Theta(1:W_stock);
Theta_day=Theta(W_stock+1);
for j=1:numTokens
    Theta_tweet(:,j)=Theta(W_stock+1+(j-1)*W_tweet+1:W_stock+1+j*W_tweet);
end

%Evaluate test
Y_test=[];
for ii=101:140
    startdate=char(tweetdate(ii,:));
    findindex=0;
    for j=1:length(stockdate)
        if isequal(char(stockdate(j,:)),startdate)
            findindex=j;
        end
    end
    Y_test=[Y_test;stocksval(findindex,:)];
end

numDays_test=40;
numPoints=size(stocksval,2);
X_test=perhourtweets(:,numDays*24+1:size(perhourtweets,2));
% for i=1:size(X_test,2)
%     X_test(:,i)=X_test(:,i)/sum(X_test(:,i));
% end
Y_day_test=sum(Y_test,2)/numPoints;
X_LMS_test=zeros((numDays_test-1)*(numPoints-W_stock+1)/2,length(Theta));
Y_LMS_test=zeros(size(X_LMS_test,1),1);
count=0;
for i=2:numDays_test
    for t=W_stock+1:2:numPoints
        count=count+1;
        Y_LMS_test(count)=Y_test(i,t);
        X_LMS_test(count,:)=[Y_test(i,t-1:-1:t-W_stock),Y_day_test(i-1),reshape(X_test(1:numTokens,(i-1)*24+(t+1)/2+8:-1:(i-1)*24+(t+1)/2+9-W_tweet)',1,numTokens*W_tweet)]; 
    end
end
Y_predict_test=X_LMS_test*Theta;
Y_temp_predict_test=reshape(Y_predict_test,numDays_test-1,(numPoints-W_stock+1)/2);
Y_temp_predict_test=[Y_test(2:numDays_test,W_stock-1),Y_temp_predict_test];
stockgrowth_predict_test=diff(Y_temp_predict_test,1,2);
stockgrowth_predict_test=(stockgrowth_predict_test>0);
Y_temp_test=Y_test(2:numDays_test,W_stock-1:2:numPoints);
stockgrowth_test=diff(Y_temp_test,1,2);
stockgrowth_test=(stockgrowth_test>0);
growtherror_test=sum(sum(abs(stockgrowth_test-stockgrowth_predict_test)))/(size(stockgrowth_test,1)*size(stockgrowth_test,2));



test_error=sum((Y_predict_test-Y_LMS_test).^2)/length(Y_LMS_test);

train_error
test_error
growtherror_test
growtherror


save('armaoutput.mat','Theta','Theta_stock','Theta_day','Theta_tweet','W_tweet','W_stock','train_error','test_error','growtherror_test','growtherror')
